import pymysql


class MySqlHelper():
    def __init__(self, host, user, password, database, port=3306, charset='utf8'):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.charset = charset
        self.connection = None
        self.cursor = None

    def connect(self):
        self.connection = pymysql.connect(host=self.host,
                                          user=self.user,
                                          password=self.password,
                                          database=self.database,
                                          port=self.port,
                                          charset=self.charset
                                          )
        self.cursor = self.connection.cursor()

    def close(self):
        self.cursor.close()
        self.connection.close()

    def insert(self, sql, params: list):
        count = None
        try:
            self.connect()
            count = self.cursor.execute(sql, params)
            self.connection.commit()
        except Exception as e:
            print(e)
            self.connection.rollback()
        finally:
            self.close()
        return count

    def update(self, sql, params: list):
        count = None
        try:
            self.connect()
            count = self.cursor.execute(sql, params)
            self.connection.commit()
        except Exception as e:
            print(e)
            self.connection.rollback()
        finally:
            self.close()
        return count

    def fetch_one(self, sql, params: list):
        result = None
        try:
            self.connect()
            self.cursor.execute(sql, params)
            result = self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()
        return result


class Person():
    def register(self):
        sql = "insert into taobao_users(username, password) values(%s, %s);"
        username = input("请输入用户名：")
        password = input("请输入密码：")
        params = [username, password]
        helper = MySqlHelper('localhost', 'root', '123456', 'hqyj')
        count = helper.insert(sql, params)
        if count:
            print("注册成功")
        else:
            print("注册失败")

    def login(self):
        sql = "select * from taobao_users where username=%s and password=%s;"
        username = input("请输入用户名：")
        password = input("请输入密码：")
        params = [username, password]
        helper = MySqlHelper('localhost', 'root', '123456', 'hqyj')
        result = helper.fetch_one(sql, params)
        if result:
            print("登录成功")
        else:
            # 判断是用户名错误还是密码错误
            if helper.fetch_one("select * from taobao_users where username=%s;", [username]):
                print("密码错误")
            else:
                print("用户名错误")

    def update(self):
        helper = MySqlHelper('localhost', 'root', '123456', 'hqyj')
        username = input("请输入用户名：")
        sql = "select * from taobao_users where username=%s;"
        result = helper.fetch_one(sql, [username])
        if not result:
            print("用户不存在")
            return

        password = input("请输入新密码：")
        params = [password, username]
        sql = "update taobao_users set password=%s where username=%s;"
        count = helper.update(sql, params)
        if count:
            print("修改成功")
        else:
            print("修改失败")


if __name__ == '__main__':

    while True:
        print("=====欢迎来到淘宝=====")
        print("1. 注册")
        print("2. 登录")
        print("3. 修改密码")
        print("0. 退出")
        choice = input("请输入你的选择：")
        if choice == '1':
            person = Person()
            person.register()
        elif choice == '2':
            person = Person()
            person.login()
        elif choice == '3':
            person = Person()
            person.update()
        elif choice == '0':
            break
        else:
            print("输入错误，请重新输入")
